var Query = RichBase.extend({
	statics : {
		currentPage : 0,
		last : "",
		//oData : {},
		queryState : {
			loading : '<li class="loading" style="text-align:center; line-height:1; height:400px;"><p style="color:#aeaeae; font-size:12px; padding-top:300px;">加载数据...</p><img src="images/other/loading_1.gif" alt="" /></li>',
			empty : '<li class="loading" style="text-align:center; line-height:1; height:400px;"><p style="color:#aeaeae; font-size:12px; padding-top:300px;">没有数据...</p></li>',
			fail : '<li class="loading" style="text-align:center; line-height:1; height:400px;"><p style="color:#aeaeae; font-size:12px; padding-top:300px;">请求失败...</p></li>',
			unlogin : '<li class="loading" style="text-align:center; line-height:1; height:400px;"><p style="color:#aeaeae; font-size:12px; padding-top:300px;">登录过期，请重新登录...</p></li>',
			timeout : '<li class="loading" style="text-align:center; line-height:1; height:400px;"><p style="color:#aeaeae; font-size:12px; padding-top:300px;">网络请求超时...</p></li>',
			serverError : '<li class="loading" style="text-align:center; line-height:1; height:400px;"><p style="color:#aeaeae; font-size:12px; padding-top:300px;">请求出错...</p></li>'
		},
		isObjEmpty : function(obj){
			for(var i in obj){
				return false;
			}
			return true;
		}
	}, 
	EVENTS : {
		"click" : {
			"#pagenavW .pageBtn" : "onPrevNextClick"
		}
	},
	init : function(opt){
		this.prevBtn = $("#prevPageBtn");
		this.nextBtn = $("#nextPageBtn");
	},
	setCurPage : function(page){
		this.statics.currentPage = page;
	},
	getCurPage : function(){
		return this.statics.currentPage;
	},
	onPrevNextClick : function(that,e){
		var tarBtn = $(e.currentTarget);
		var paa ="0";
		if(tarBtn.hasClass("disable")) return false;
		var curPage = that.statics.currentPage;
		var t = $("#search_text").val();
		var g =$(".fb_price").attr("qu");
		if(g=="1"){
			if(t=="分销商名称/账号"){
				var txt="";
			}
			else{
				var txt = t;
			}
		}
		else{
			var txt="";
		}
		//console.log(txt)
		var pid = $(".pid").val()
		var sid = $(".sid").val()
		var all = $(".nextBtn").attr("total");
		var toPage = tarBtn.hasClass("prevBtn") ? (curPage-1) : (curPage+1);
		$(".pri_input").each(function(i,ele){
			var pa = $(this).attr("pa");
			if(pa=="0"){
				paa ="1"
			}
		})
		if(paa=="1"){
			var flag = confirm('您尚有分销价格未保存，是否进行保存？');
			if(flag){
				$(".pri_input").each(function(i,ele){
					var price = $(this).val();
					if(price=="-1"){// plus
						price = "";
					}
					$(this).val(price);
				});
				var postData = $("form#form").serialize();
				var url = "adjusting_dt.php";
				$.ajax({
					type:'POST',url: url,data: postData, dataType:'json',
					//type:'POST',url: 'http://www.12301.cc/admin/pppp.php',data: postData, dataType:'json',
				}).done(function(res) {
					if(res.status=="success"){
						//alert(res.msg);
						PFT_GLOBAL.U.Alert("success",'<p style="width:240px">'+res.msg+'</p>');
						$(".chose_l").text(res.channel);
					}
					else{
						PFT_GLOBAL.U.Alert("fail",'<p style="width:240px">'+res.msg+'</p>');
						//alert(res.msg);
					}
				})	
				if(tarBtn.hasClass("prevBtn")){
				that.nextBtn.removeClass("disable");
					qQuery.query({
						last:toPage,
						ty:1,
						pid : pid,
						sid : sid,
						dname :txt
					});
				if(toPage=="1"){
					that.prevBtn.addClass("disable");
					return false;
				}
			}else{
				that.prevBtn.removeClass("disable");
				if(toPage<=all){
					qQuery.query({
						last:toPage,
						ty:2,
						pid : pid,
						sid : sid,
						dname :txt
					});
				}else{
					that.nextBtn.addClass("disable");
					PFT_GLOBAL.U.Alert("fail",'<p style="width:240px">抱歉最后一页</p>');
				}
			}
			}
			else{
				if(tarBtn.hasClass("prevBtn")){
				that.nextBtn.removeClass("disable");
					qQuery.query({
						last:toPage,
						ty:1,
						pid : pid,
						sid : sid,
						dname :txt
					});
				if(toPage=="1"){
					that.prevBtn.addClass("disable");
					return false;
				}
			}else{
				that.prevBtn.removeClass("disable");
				if(toPage<=all){
					qQuery.query({
						last:toPage,
						ty:2,
						pid : pid,
						sid : sid,
						dname :txt
					});
				}else{
					that.nextBtn.addClass("disable");
					PFT_GLOBAL.U.Alert("fail",'<p style="width:240px">抱歉最后一页</p>');
				}
			}
				$(".pri_input").each(function(i,ele){
					var pa = $(this).attr("pa","1");
				})
			}
		}else{
			if(tarBtn.hasClass("prevBtn")){
				that.nextBtn.removeClass("disable");
					qQuery.query({
						last:toPage,
						ty:1,
						pid : pid,
						sid : sid,
						dname :txt
					});
				if(toPage=="1"){
					that.prevBtn.addClass("disable");
					return false;
				}
			}else{
				that.prevBtn.removeClass("disable");
				if(toPage<=all){
					qQuery.query({
						last:toPage,
						ty:2,
						pid : pid,
						sid : sid,
						dname :txt
					});
				}else{
					that.nextBtn.addClass("disable");
					PFT_GLOBAL.U.Alert("fail",'<p style="width:240px">抱歉最后一页</p>');
				}
			}
		}
	},
	query : function(opt){
		var that = this;
		var opt = opt || {};
		var pageSize = opt.pageSize || 20;
		var last = opt.last || "";
		var dname = opt.dname || "";
		var dstatus = opt.dstatus || "-1";
		var currentPage = opt.last || "0";
		var type = opt.type || "0";
		var ty = opt.ty || "2";
		var sid = opt.sid;
		var pid = opt.pid;
		var t = opt.t;
		var qu = opt.qu;
		var ttimeout = opt.ttimeout || 20 * 60 * 1000;
		var data = {
			pageSize : pageSize,
			//last : last,
			txt : dname,
			currentPage : last,
			sid : sid,
			pid : pid,
			act:"lists"
		};
		//console.log("ty"+" "+ty)
		var listUl = $("#mlistUl");
		var pagenavW = $("#pagenavW");
		var status = this.statics.queryState;
		if(ty=="2"){
			PFT.Ajax({
				//url : "http://www.12301.cc/alliance_mem.html?lists=get",
				//url : "channels_conf.html",
				url : "adjusting_o.html",
				type : "GET",
				dataType : "json",
				ttimeout : ttimeout,
				data : data,
				loading : function(){
					listUl.html(status["loading"]);
					pagenavW.hide();
				},
				removeLoading : function(res){
					that.statics.last = "";
					that.statics.currentPage += 1;
					listUl.html("");
					pagenavW.show();
				},
				timeout : function(){ listUl.html(status["timeout"]);},
				serverError : function(){ listUl.html(status["serverError"]);} 
			},function(res){
				var lists = res.list;
				//console.log(lists)
				var last = res.total;
				var totalPage = res.totalPage;
					if(!that.statics.isObjEmpty(lists)){
						//that.statics.oData[that.statics.currentPage] = res;
						that.buildHtml(res,t,qu);
					}else{
						listUl.html(status["empty"]);
						$("#pagenavW").hide();
					}
			})
		}
		else{
			
			PFT.Ajax({
			//url : "http://www.12301.cc/alliance_mem.html?lists=get",
			//url : "channels_conf.html",
			url : "adjusting_o.html",
			type : "GET",
			dataType : "json",
			ttimeout : ttimeout,
			data : data,
			loading : function(){
				listUl.html(status["loading"]);
				pagenavW.hide();
			},
			removeLoading : function(res){
				that.statics.last = "";
				that.statics.currentPage -= 1;
				listUl.html("");
				pagenavW.show();
			},
			timeout : function(){ listUl.html(status["timeout"]);},
			serverError : function(){ listUl.html(status["serverError"]);} 
		},function(res){
			var lists = res.list;
			var last = res.total;
			var totalPage = res.totalPage;
				if(!that.statics.isObjEmpty(lists)){
					that.buildHtml(res,t,qu);
				}else{
					listUl.html(status["empty"]);
					$("#pagenavW").hide();
				}
		})
		}
	},
	buildHtml : function(res,t,qu){
		var that = this;
		var lists = res.list;
		var str = "";
		var last = res.last;
		var data = lists;
		var total = res.total;
		var totalPage = res.totalPage || "0";
		$(".nextBtn").attr("total",totalPage);
		//console.log(totalPage)
		if(totalPage=="1"){
			$(".nextBtn").addClass("disable");
		}else{
			$(".nextBtn").removeClass("disable");
		}
		for(var i in data){
			str +=   '<tbody>';
			str +=   '<tr>';
			str +=   '	<td class="txtlf">'+data[i]["dname"]+'（ID:'+data[i]["account"]+'）</td>';
			str +=   '	<td class="pri">';
			str +=   '	<input type="text" style="" idd="'+data[i]["id"]+'" pa="1" name="pri['+data[i]["id"]+']" js="'+data[i]["js"]+'" class="formInp pri_input" value="'+data[i]["js"]+'">';
			str +=   '	</td>';
			str +=   '</tr>';
			str +=   '</tbody>';
		}
		$("#mlistUl").html(str);
		$(".pri_input").each(function(i,ele){
			var price = $(this).val();
			if(price=="-1"){// plus
				price = "";
			}
			$(this).val(price);
		});
		$("#total_num").text("共"+totalPage+"页"); 
		if(t=="1"){
			that.statics.currentPage = 1;
			$("#whichPageNum").text(that.statics.currentPage);
		}else{
			$("#whichPageNum").text(that.statics.currentPage);
		}
		if(qu=="1"){
			that.statics.currentPage = 1;
			$("#whichPageNum").text(that.statics.currentPage);
		}
		 $('#mlistUl tbody:odd').css("backgroundColor","#FAFAFA");
		 $("html,body").animate({"scrollTop":0})
	}
	
});





